package com.baidu.shop.service.impl;

import com.baidu.shop.base.BaseApiService;
import com.baidu.shop.base.Result;
import com.baidu.shop.dto.SpecGroupDTO;
import com.baidu.shop.dto.SpecParamDTO;
import com.baidu.shop.entity.SpecGroupEntity;
import com.baidu.shop.entity.SpecParamEntity;
import com.baidu.shop.mapper.SpecParamMapper;
import com.baidu.shop.mapper.SpecificationMapper;
import com.baidu.shop.service.SpecificationService;
import com.baidu.shop.utils.BaiduBeanUtil;
import com.baidu.shop.utils.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description:
 * @ClassName: SpecificationServiceImpl
 * @Author: User
 * @date: 2020.09.03 11:45
 * @version: 1.0
 */
@RestController
@Transactional(readOnly = false)
public class SpecificationServiceImpl extends BaseApiService implements SpecificationService {
    @Resource
    private SpecificationMapper mapper;

    @Resource
    private SpecParamMapper specParamMapper;

    @Override
    @Transactional(readOnly = true)
    public Result<List<SpecGroupEntity>> getSepcGroupInfo(SpecGroupDTO specGroupDTO) {
        Example example = new Example(SpecGroupEntity.class);
        if(ObjectUtil.isNotNull(specGroupDTO.getCid())) example.createCriteria().andEqualTo("cid", specGroupDTO.getCid());
        List<SpecGroupEntity> specGroupEntities = mapper.selectByExample(example);
        return this.setResultSuccess(specGroupEntities);
    }

    @Override
    public Result add(SpecGroupDTO specGroupDTO) {
        int i = mapper.insertSelective(BaiduBeanUtil.copyProperties(specGroupDTO, SpecGroupEntity.class));
        return this.setResultSuccess();
    }

    @Override
    public Result edit(SpecGroupDTO specGroupDTO) {
        int i =  mapper.updateByPrimaryKeySelective(BaiduBeanUtil.copyProperties(specGroupDTO, SpecGroupEntity.class));
        return this.setResultSuccess();
    }

    @Override
    public Result delete(Integer id) {

        Example example = new Example(SpecParamEntity.class);
        example.createCriteria().andEqualTo("groupId", id);
        List<SpecParamEntity> specParamEntities = specParamMapper.selectByExample(example);
        String msg="";
        if (!specParamEntities.isEmpty()){
            for (SpecParamEntity entity : specParamEntities) {
                msg+="["+entity.getName()+"]";
            }
            return this.setResultError(404, "该分组下有被参数"+msg+"绑定无法被删除");
        }
        int i = mapper.deleteByPrimaryKey(id);
        return this.setResultSuccess();
    }

    @Override
    @Transactional(readOnly = true)
    public Result<List<SpecParamEntity>> getSpecParamInfo(SpecParamDTO specParamDTO) {
        if (ObjectUtil.isNull(specParamDTO)){
            return  this.setResultError("参数为空");
        }
        //if(ObjectUtil.isNull(specParamDTO.getGroupId()))  return  this.setResultError("分组参数为空");
        Example example = new Example(SpecParamEntity.class);
        Example.Criteria criteria = example.createCriteria();
        if (ObjectUtil.isNotNull(specParamDTO.getGroupId()))criteria.andEqualTo("groupId", specParamDTO.getGroupId());
        if (ObjectUtil.isNotNull(specParamDTO.getCid()))criteria.andEqualTo("cid", specParamDTO.getCid());
        if (ObjectUtil.isNotNull(specParamDTO.getSearching()))criteria.andEqualTo("searching", specParamDTO.getSearching());
        if (ObjectUtil.isNotNull(specParamDTO.getGeneric()))criteria.andEqualTo("generic", specParamDTO.getGeneric());

        List<SpecParamEntity> specParamEntities = specParamMapper.selectByExample(example);
        return this.setResultSuccess(specParamEntities);
    }

    @Override
    public Result addSpecParam(SpecParamDTO specParamDTO) {
        int i = specParamMapper.insertSelective(BaiduBeanUtil.copyProperties(specParamDTO, SpecParamEntity.class));
        return this.setResultSuccess();
    }

    @Override
    public Result editSpecParam(SpecParamDTO specParamDTO) {
        int i =  specParamMapper.updateByPrimaryKeySelective(BaiduBeanUtil.copyProperties(specParamDTO, SpecParamEntity.class));
        return this.setResultSuccess();
    }

    @Override
    public Result deleteSpecParam(Integer id) {
        int i = specParamMapper.deleteByPrimaryKey(id);
        return this.setResultSuccess();
    }
}
